/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-07-22     RT-Thread    first version
 */
#include "board.h"
#include <rtthread.h>
#include <rtdevice.h>

//#include "lcd_300x166_32bit.h"

#include "drv_lcd.h"
#include "drv_touch.h"
static struct rt_semaphore sem_lock;
static struct touch_message touch_msg = {0};

void touch_isr_notice(int state)
{
	rt_sem_release(&sem_lock);
}

static void test_lcd_touch_task(void *param)
{
	int color_flag = 0;
	uint32_t color = BLUE;
	uint32_t touch_x = 0,touch_y = 0;
	
	rt_sem_init(&sem_lock,"lock",1,RT_IPC_FLAG_FIFO);
	
    drv_lcd_init();
    drv_touch_init();
	//touch_isr_callback_set(touch_isr_notice);
	
	board_lcd_box(0,0,LCD_WIDTH,LCD_HEIGHT,WHITE);
	board_lcd_box(20,20,60,60,RED);
	board_lcd_box(100,20,60,60,GREEN);
	board_lcd_box(180,20,60,60,BLUE);
	//hw_lcd_flush(0,0,300,166,(void*)gImage_lcd_300x166_32bit);
	while(1)
	{
		//rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
		rt_thread_mdelay(10);
		board_touch_read_status(&touch_msg);
		if( (touch_msg.event == TOUCH_EVENT_DOWN) || (touch_msg.event == TOUCH_EVENT_MOVE) )
		{
			touch_x = touch_msg.x_coordinate;
			touch_y = touch_msg.y_coordinate;
			if( (touch_x < 100) && (touch_y < 100) )
			{
				color_flag++;
				color_flag%=3;
				switch(color_flag)
				{
					case 0:
						color = RED;
						break;
					case 1:
						color = GREEN;
						break;
					case 2:
						color = BLUE;
						break;
				}
				board_lcd_box(0,0,LCD_WIDTH,LCD_HEIGHT,WHITE);
				
				board_lcd_box(20,20,60,60,RED);
				
				board_lcd_box(100,20,60,60,GREEN);
				
				board_lcd_box(180,20,60,60,BLUE);
				
				rt_thread_mdelay(500);
			}
			board_lcd_box(touch_x,touch_y,8,8,color);
			rt_kprintf("X:%d Y:%d  %d\n", touch_msg.x_coordinate, touch_msg.y_coordinate, touch_msg.event);
		}else
		{
			
		}
	}
}

void test_lcd_touch(void)
{
	rt_thread_t tid;
	
	tid = rt_thread_create("lcd_test",test_lcd_touch_task, NULL ,2048,  30, 10);
	if (tid != RT_NULL)
			rt_thread_startup(tid);
}

